package model

import (
	"github.com/gomodule/redigo/redis"
	"strconv"
	"time"
	"tizhi_front/nspel"
)

type Course struct {
	Id                int64     `gorm:"primary_key;column:id"`
	Name              string    `gorm:"column:name"`
	LabelId           int64     `gorm:"column:label_id"`
	Label             string    `gorm:"column:label"`
	Difficulty        float64   `gorm:"column:difficulty"`
	Participants      int64     `gorm:"column:participants"`
	VideoKey          string    `gorm:"column:video_key"`
	VideoPic          string    `gorm:"column:video_pic"`
	VideoTimeLength   int64     `gorm:"column:video_time_length"`
	ShopAddress       string    `gorm:"column:shop_address"`
	Description       string    `gorm:"column:description"`
	PicUrl            string    `gorm:"column:pic_url"`
	TrainTime         int64     `gorm:"column:train_time"`
	DetailDescription string    `gorm:"column:detail_description"`
	Level0            bool      `gorm:"column:level_0"`
	Level1            bool      `gorm:"column:level_1"`
	Level2            bool      `gorm:"column:level_2"`
	Level3            bool      `gorm:"column:level_3"`
	Level4            bool      `gorm:"column:level_4"`
	Level5            bool      `gorm:"column:level_5"`
	Level6            bool      `gorm:"column:level_6"`
	Male              bool      `gorm:"column:male"`
	Female            bool      `gorm:"column:female"`
	Grade11           bool      `gorm:"column:grade_11"`
	Grade12           bool      `gorm:"column:grade_12"`
	Grade13           bool      `gorm:"column:grade_13"`
	Grade14           bool      `gorm:"column:grade_14"`
	Grade15           bool      `gorm:"column:grade_15"`
	Grade16           bool      `gorm:"column:grade_16"`
	Grade21           bool      `gorm:"column:grade_21"`
	Grade22           bool      `gorm:"column:grade_22"`
	Grade23           bool      `gorm:"column:grade_23"`
	Grade31           bool      `gorm:"column:grade_31"`
	Grade32           bool      `gorm:"column:grade_32"`
	Grade33           bool      `gorm:"column:grade_33"`
	CreateAt          time.Time `gorm:"column:create_at"`
	UpdateAt          time.Time `gorm:"column:update_at"`
}

func (*Course) TableName() string {
	return "course"
}

type CourseE struct{}

func (c *CourseE) SelectAllByLabelIdLevelSexGrade(labelId int64, level string, sex string, grade string) (courses []Course0, err error) {
	key := "course0.label_id:" + strconv.FormatInt(labelId, 10) + "." + level + "." + sex + "." + grade + ".all"
	err = nspel.RedisGetGob(key, &courses)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select([]string{"id", "name", "label_id", "difficulty", "participants", "video_key", "video_pic", "video_time_length", "shop_address", "description"}).Where("label_id = ?", labelId).Where(level+" = ?", true).Where(sex+" = ?", true).Where(grade+" = ?", true).Find(&courses).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, courses)
	return
}

func (c *CourseE) SelectAllById(id []int64) (courses []Course0, err error) {
	err = nspel.GetOrm().Select([]string{"id", "name", "label_id", "difficulty", "participants", "video_key", "video_pic", "video_time_length", "shop_address", "description"}).Where("id in (?)", id).Find(&courses).Error
	return
}

func (c *CourseE) SelectAllByLabelId(labelId int64) (courses []Course0, err error) {
	key := "course0.label_id:" + strconv.FormatInt(labelId, 10) + ".all"
	err = nspel.RedisGetGob(key, &courses)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select([]string{"id", "name", "label_id", "difficulty", "participants", "video_key", "video_pic", "video_time_length", "shop_address", "description"}).Where("label_id = ?", labelId).Find(&courses).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, courses)
	return
}

func (c *CourseE) SelectAllOrderByLabelIdAsc() (courses []Course0, err error) {
	key := "course0.label_id.asc.all"
	err = nspel.RedisGetGob(key, &courses)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select([]string{"id", "name", "label_id", "difficulty", "participants", "video_key", "video_pic", "video_time_length", "shop_address", "description"}).Order("label_id").Find(&courses).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, courses)
	return
}

type Course0 struct {
	Id              int64   `gorm:"primary_key;column:id"`
	Name            string  `gorm:"column:name"`
	LabelId         int64   `gorm:"column:label_id"`
	Difficulty      float64 `gorm:"column:difficulty"`
	Participants    int64   `gorm:"column:participants"`
	VideoKey        string  `gorm:"column:video_key"`
	VideoPic        string  `gorm:"column:video_pic"`
	VideoTimeLength int64   `gorm:"column:video_time_length"`
	ShopAddress     string  `gorm:"column:shop_address"`
	Description     string  `gorm:"column:description"`
}

func (*Course0) TableName() string {
	return "course"
}

func (c *Course0) SelectByLabelIdLevelSexGrade(labelId int64, level string, sex string, grade string) (err error) {
	key := "course0.label_id:" + strconv.FormatInt(labelId, 10) + "." + level + "." + sex + "." + grade
	err = nspel.RedisGetGob(key, c)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select([]string{"id", "name", "label_id", "difficulty", "participants", "video_key", "video_pic", "video_time_length", "shop_address", "description"}).Where("label_id = ?", labelId).Where(level+" = ?", true).Where(sex+" = ?", true).Where(grade+" = ?", true).First(c).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, c)
	return
}

type Course1 struct {
	Name              string  `gorm:"column:name"`
	LabelId           int64   `gorm:"column:label_id"`
	Difficulty        float64 `gorm:"column:difficulty"`
	Participants      int64   `gorm:"column:participants"`
	VideoKey          string  `gorm:"column:video_key"`
	VideoPic          string  `gorm:"column:video_pic"`
	VideoTimeLength   int64   `gorm:"column:video_time_length"`
	ShopAddress       string  `gorm:"column:shop_address"`
	Description       string  `gorm:"column:description"`
	PicUrl            string  `gorm:"column:pic_url"`
	TrainTime         int64   `gorm:"column:train_time"`
	DetailDescription string  `gorm:"column:detail_description"`
}

func (*Course1) TableName() string {
	return "course"
}

func (c *Course1) SelectById(id int64) (err error) {
	key := "course1.id:" + strconv.FormatInt(id, 10)
	err = nspel.RedisGetGob(key, c)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select([]string{"id", "name", "label_id", "difficulty", "participants", "video_key", "video_pic", "video_time_length", "shop_address", "description", "pic_url", "train_time", "detail_description"}).Where("id = ?", id).First(c).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, c)
	return
}

type Course2 struct {
	Id int64 `gorm:"primary_key;column:id"`
}

func (*Course2) TableName() string {
	return "course"
}

func (c *Course2) SelectById(id int64) (err error) {
	key := "course2.id:" + strconv.FormatInt(id, 10)
	err = nspel.RedisGetGob(key, c)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select("id").Where("id = ?", id).First(c).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, c)
	return
}
